/*
 * libroxsd - a portable, lightweight XSD validation library
 * Copyright (C) 2012  Emmanuel Deloget
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

#include <string.h>

#include "roxsd-list.h"
#include "roxsd-compiler-xsdtype.h"

void roxsdi_xsdtype_free(xsdtype_t *t)
{
	while (t->children) {
		xsdtype_t *child;

		child = t->children;
		t->children = list_next(t->children);

		roxsdi_xsdtype_free(child);
	}

	free(t);
}

xsdtype_t *roxsdi_xsdtype_new(char *ename, char *etype, int minocc, int maxocc, int ttype)
{
	xsdtype_t *type;

	type = malloc(sizeof(xsdtype_t));
	if (type) {
		type->name = ename;
		type->tname = etype;
		type->type = ttype;
		type->minocc = minocc;
		type->maxocc = maxocc;
		type->children = NULL;
	}
	return type;
}

xsdtype_t *roxsdi_xsdtype_add_element(xsdtype_t *type, char *ename, char *etype)
{
	xsdtype_t *sym = NULL;

	if (!type || !ename || !etype)
		return NULL;

	switch (type->type) {
	case ROXSDI_TYPE_CHOICE:
		sym = roxsdi_element_new(ename, etype);
		(void)roxsdi_xsdtype_add_child(type, sym);
		break;
	case ROXSDI_TYPE_SEQUENCE:
		sym = roxsdi_element_new(ename, etype);
		(void)roxsdi_xsdtype_add_child(type, sym);
		break;
	default:
		return NULL;
	}
	return sym;
}

xsdtype_t *roxsdi_xsdtype_find(xsdtype_t *head, char *etype)
{
	xsdtype_t *cur = head;

	while (cur) {
		if (strcmp(cur->tname, etype) == 0)
			return cur;
		cur = list_next(cur);
	}
	return NULL;
}

